<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 订单打印
 * @package  htdocs
 * @subpackage  quanliadmin
 * @author   create by allen.qiang
 */

//引入字典类
quanli_require('module/base/system/itemHandler.class.php');

class pa_orderPrint {

    public function execute() {

        global $G_PGLOB;

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1: //显示自营订单列表页面
                    $this->orderPrintList();
                    break;
                case 2: //显示澳粒订单列表页面
                    $this->ALorderPrintList();
                    break;

                default:
                    $G_PGLOB->smarty->assign('msg', '参数错误，请审核后再操作');
                    $G_PGLOB->smarty->display('comm/error.tpl');
            }
        }
    }


    /**
     * 显示自营订单列表页面
     */
    private function orderPrintList() {

        global $G_PGLOB;

        //实例化字典类
        $itemHandler = new itemHandler();

        //配送开始时间数组
        $sendStartTime_array = array();

        //配送结束时间数组
        $sendEndTime_array = array();

        //配送时间参数
        $itemStart_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );

        //查询字典表信息
        $temp_array = $itemHandler->searchItem( $itemStart_array );

        if( $temp_array ){
            foreach( $temp_array as $val ){
                if($val['itemCategoryID'] == 9){
                    $sendStartTime_array[]['itemName'] = $val['itemName'];
                }

                if($val['itemCategoryID'] == 10){
                    $sendEndTime_array[]['itemName'] = $val['itemName'];
                }

            }
        }

        //保存配送结束时间最晚的时间
        $end_time = '';
        if($sendEndTime_array){
            $result_data = end($sendEndTime_array);
            $end_time = $result_data['itemName'];
        }

        $billPlatform = 1;//自营订单
        $G_PGLOB->smarty->assign_by_ref('billPlatform',$billPlatform);
        $G_PGLOB->smarty->assign_by_ref('end_time',$end_time);
        $G_PGLOB->smarty->assign_by_ref('sendStartTime_array',$sendStartTime_array);//配送开始时间
        $G_PGLOB->smarty->assign_by_ref('sendEndTime_array',$sendEndTime_array);//配送结束时间
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(20512));
        $G_PGLOB->smarty->assign('permission_20512',$G_PGLOB->has_permission(20512));
        $G_PGLOB->smarty->assign('currentDate',$G_PGLOB->sys_date);//当前日期

        $table_id = 2;

        $flex_array = array(
            array('display' => '订单编号','width' => 140,'align' => 'center'),
            array('display' => '订单标识', 'width' => 60,),
            array('display' => '订单类型','width' => 80),
            array('display' => '订单状态','width' => 80),
            array('display' => '配送单打印次数','width' => 100),
            array('display' => '配送单打印时间', 'name' => 'print_time', 'sortable' => true, 'width' => 140,'align'=> 'center'),
            array('display' => '配送任务清单打印次数','width' => 130),
            array('display' => '配送日期','width' => 100,'align' => 'center'),
            array('display' => '配送开始时间', 'name' => 'send_start_time', 'sortable' => true, 'width' => 80),
            array('display' => '配送结束时间','width' => 80),
            array('display' => '配送站','width' => 150),
            array('display' => '配送员','width' => 80, 'name' => 'distribution_technician_id', 'sortable' => true),
            array('display' => '城市','width' => 80),
            array('display' => '区','width' => 80),
            array('display' => '收货地址','width' => 300),
            array('display' => '收货人','width' => 100),
            array('display' => '收货人手机','width' => 100),
            array('display' => '固定电话','width' => 100),
            array('display' => '发票','width' => 80),
            array('display' => '蛋糕数量','width' => 80),
            array('display' => '线下付款方式','width' => 100),
            array('display' => '应收金额', 'width' => 80),
            array('display' => '配送备注', 'width' => 120),
        ); //flexigrid插件表格  end

        quanli_require('module/base/system/sysFlexigridLog.class.php');

        $sysFlexigridLog = new sysFlexigridLog();

        $arg_array = array(
            'table_id' => $table_id,
            'userID' => $G_PGLOB->currentUserID,
            'sortname' => 'last_cid',
            'sortorder' => 'ASC'
        );
        $flexigrid_array = $sysFlexigridLog->searchFlexigridInfo($arg_array);

        if($flexigrid_array){
            $flex_array = $flexigrid_array;
        }else{
            $arg_array = array(
                'table_id' => $table_id ,
                'userID' => $G_PGLOB->currentUserID,
                'data' => $flex_array
            );
            $sysFlexigridLog->editFlexigrid($arg_array);
        }

        $G_PGLOB->smarty->assign_by_ref('table_id', $table_id);
        $G_PGLOB->smarty->assign_by_ref('fixed_json', json_encode($flex_array));

        $G_PGLOB->smarty->display('send/orderPrintList.tpl');


    }

    /**
     * 显示澳粒订单列表页面
     */
    private function ALorderPrintList() {

        global $G_PGLOB;

        //实例化字典类
        $itemHandler = new itemHandler();

        //配送开始时间数组
        $sendStartTime_array = array();

        //配送结束时间数组
        $sendEndTime_array = array();

        //配送时间参数
        $itemStart_array = array(
            'useFlg' => 1,
            'sortname' => 'itemID',
            'sortorder' => 'ASC'
        );

        //查询字典表信息
        $temp_array = $itemHandler->searchItem( $itemStart_array );

        if( $temp_array ){
            foreach( $temp_array as $val ){
                if($val['itemCategoryID'] == 9){
                    $sendStartTime_array[]['itemName'] = $val['itemName'];
                }

                if($val['itemCategoryID'] == 10){
                    $sendEndTime_array[]['itemName'] = $val['itemName'];
                }

            }
        }

        //保存配送结束时间最晚的时间
        $end_time = '';
        if($sendEndTime_array){
            $result_data = end($sendEndTime_array);
            $end_time = $result_data['itemName'];
        }

        $billPlatform = 2;//澳粒订单
        $G_PGLOB->smarty->assign_by_ref('billPlatform',$billPlatform);
        $G_PGLOB->smarty->assign_by_ref('end_time',$end_time);
        $G_PGLOB->smarty->assign_by_ref('sendStartTime_array',$sendStartTime_array);//配送开始时间
        $G_PGLOB->smarty->assign_by_ref('sendEndTime_array',$sendEndTime_array);//配送结束时间
        $G_PGLOB->smarty->assign('edit_permission', $G_PGLOB->has_permission(20516));
        $G_PGLOB->smarty->assign('permission_20516', $G_PGLOB->has_permission(20516));
        $G_PGLOB->smarty->assign('currentDate',$G_PGLOB->sys_date);//当前日期

        $table_id = 3;

        $flex_array = array(
            array('display' => '订单编号','width' => 140,'align' => 'center'),
            array('display' => '订单标识', 'width' => 60,),
            array('display' => '订单类型','width' => 80),
            array('display' => '订单状态','width' => 80),
            array('display' => '配送单打印次数','width' => 100),
            array('display' => '配送单打印时间', 'name' => 'print_time', 'sortable' => true, 'width' => 140,'align'=> 'center'),
            array('display' => '配送任务清单打印次数','width' => 130),
            array('display' => '配送日期','width' => 100,'align' => 'center'),
            array('display' => '配送开始时间', 'name' => 'send_start_time', 'sortable' => true, 'width' => 80),
            array('display' => '配送结束时间','width' => 80),
            array('display' => '配送站','width' => 150),
            array('display' => '配送员','width' => 80, 'name' => 'distribution_technician_id', 'sortable' => true),
            array('display' => '城市','width' => 80),
            array('display' => '区','width' => 80),
            array('display' => '收货地址','width' => 300),
            array('display' => '收货人','width' => 100),
            array('display' => '收货人手机','width' => 100),
            array('display' => '固定电话','width' => 100),
            array('display' => '发票','width' => 80),
            array('display' => '蛋糕数量','width' => 80),
            array('display' => '线下付款方式','width' => 100),
            array('display' => '应收金额', 'width' => 80),
            array('display' => '配送备注', 'width' => 120),
        ); //flexigrid插件表格  end

        quanli_require('module/base/system/sysFlexigridLog.class.php');

        $sysFlexigridLog = new sysFlexigridLog();

        $arg_array = array(
            'table_id' => $table_id,
            'userID' => $G_PGLOB->currentUserID,
            'sortname' => 'last_cid',
            'sortorder' => 'ASC'
        );
        $flexigrid_array = $sysFlexigridLog->searchFlexigridInfo($arg_array);

        if($flexigrid_array){
            $flex_array = $flexigrid_array;
        }else{
            $arg_array = array(
                'table_id' => $table_id ,
                'userID' => $G_PGLOB->currentUserID,
                'data' => $flex_array
            );
            $sysFlexigridLog->editFlexigrid($arg_array);
        }

        $G_PGLOB->smarty->assign_by_ref('table_id', $table_id);
        $G_PGLOB->smarty->assign_by_ref('fixed_json', json_encode($flex_array));

        $G_PGLOB->smarty->display('send/orderPrintList.tpl');


    }

    //配送任务清单打印
    private function orderPrint(){

        //查询条件选择了城市、配送站 则根据选择的城市、配送站查询
        if( isset($_REQUEST['deliver_cityid']) && is_numeric($_REQUEST['deliver_cityid']) && isset( $_REQUEST['distribution_id']) && is_numeric($_REQUEST['distribution_id'])){
            $_REQUEST['deliver_cityid'] = $_REQUEST['formValue']['deliver_cityid'];
            $_REQUEST['distribution_id'] = $_REQUEST['formValue']['distribution_id'];

            //查询条件选择了城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的配送站查询
        }else if(isset($_REQUEST['deliver_cityid']) && is_numeric($_REQUEST['deliver_cityid']) && !isset( $_REQUEST['distribution_id']) || !is_numeric($_REQUEST['distribution_id']) ){
            $_REQUEST['deliver_cityid'] = $_REQUEST['deliver_cityid'];
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }

            //查询条件没有选择城市、没有选择配送站 则根据选择的城市、当前登录用户权限下的城市、配送站查询
        }else{
            global $G_SHOP;
            $PS_LOGIN_SESSION =$G_SHOP->sessionCache_get('PS_LOGIN_SESSION');

            $distribution_id_array = $PS_LOGIN_SESSION['distribution'];//权限下的配送站id数组

            if( count($distribution_id_array) > 1){

                $distribution_id_arg = implode( ',',$distribution_id_array );

                $_REQUEST['extend'] = ' AND distribution_id IN ('.$distribution_id_arg.')';

            }else{
                $_REQUEST['distribution_id'] = $distribution_id_array[0];

            }
        }


        global $G_PGLOB;
        $bill_no_array = array();//订单号数组
        //引入字典类
        quanli_require('module/base/bill/billSearch.class.php');
        $billSearch = new billSearch();
        //查询订单信息
        $temp_array  = $billSearch->searchQueryBill( $_REQUEST );

        foreach( $temp_array as $key=>$val){

            $bill_no_array[] = $val['bill_no'];

            //订单查询类
            $bill_arraySearch = new billSearch();

            $arg_array = array(
                'bill_no'=>$val['bill_no']
            );
            //查询订单商品表 bill_goods
            $bill_goods_array = $bill_arraySearch->searchBillGoods( $arg_array );

            $birthday_card_array = array();
            $candle_amount_array = array();
            if( $bill_goods_array ){
                foreach( $bill_goods_array as $v){
                    if($v['birthday_card']){
                        $birthday_card_array[] = $v['birthday_card'];
                    }
                    $candle_amount_array[] = $v['candle_amount']>0?'是':'否';
                }
            }
            if( $birthday_card_array ){
                $birthday_card_str = implode(',',$birthday_card_array );
                $temp_array[$key]['birthday_card_str'] = $birthday_card_str;
            }

            if( $candle_amount_array ){
                $candle_amount_str = implode(',',$candle_amount_array);
                $temp_array[$key]['candle_amount_str'] =$candle_amount_str;
            }
        }

        $G_PGLOB->smarty->assign_by_ref('temp_array',$temp_array);
        $G_PGLOB->smarty->display('send/orderTaskList.tpl');
    }

}
